package cn.jobstart.props;

import com.alibaba.fastjson.JSONArray;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author sunke
 * @version 1.0
 * @Description
 * @date 2023-07-31 16:55
 */

@ConfigurationProperties(prefix = "file.upload")
@Data
@Component
public class FileConfig {

    private String root;
    private String tempRoot;
    private String website;
    private String baseUrl;
    private String chromeDriverPath;
    private String provinceJson;
    private String cityJson;

    private List<Province> provinceList;
    private List<City> cityList;

    /**
     * 读取文本文件
     *
     * @param fileName
     * @return
     */
    public <T> List<T> readInFile(String fileName, Class className) {
        FileReader fr = null;
        try {
            fr = new FileReader(fileName);

            BufferedReader br = new BufferedReader(fr);

            StringBuffer sf = new StringBuffer();
            String s;
            while ((s = br.readLine()) != null) {
                sf.append(s);
            }

            return JSONArray.parseArray(sf.toString(), className);


        } catch (Exception e) {
            return null;
        } finally {
            try {
                if (fr != null) {
                    fr.close();
                }

            } catch (Exception e) {

            }


        }

    }

    public List<City> getCityList() {
        if (cityList == null) {
            cityList = readInFile(getRoot() + File.separator + getCityJson(), City.class);
        }
        return cityList;
    }

    public List<Province> getProvinceList() {
        if (provinceList == null) {
            provinceList = readInFile(getRoot() + File.separator + getProvinceJson(), Province.class);
        }
        return provinceList;
    }

    /**
     * @param keyWordList
     * @return
     */
    public List<String> getCityListByProvinceList(List<String> keyWordList) {
        if (keyWordList == null || keyWordList.size() == 0) {
            return null;
        }

        List<Province> provinceList = getProvinceList();
        if (provinceList == null || provinceList.size() == 0) {
            return keyWordList;
        }

        //判断一下keyword 是不是不光包括省，就直接返回
        if (!keyWordList.stream().allMatch(item -> provinceList.stream().anyMatch(province -> province.getName().indexOf(item) >= 0))) {
            return keyWordList;
        }
        List<String> returnList = new ArrayList<>();

        List<City> cityList = getCityList();
        if (cityList == null || cityList.size() == 0) {
            return keyWordList;
        }
        //说明这里全是省
        keyWordList.stream().forEach(provinceKey -> {
            returnList.add(provinceKey);
            Province province = null;
            for (Province provinceItem : provinceList) {
                if (provinceItem.getName().indexOf(provinceKey) >= 0) {
                    province = provinceItem;
                    break;
                }
            }
            if (province != null) {

                Province finalProvince = province;
                List<String> findCityList = cityList.stream().filter(item -> item.getProID() == finalProvince.getProID()).map(City::getName).collect(Collectors.toList());
                if (findCityList != null) {
                    returnList.addAll(findCityList);
                }

            }


        });


        return returnList;


    }


}
